package com.ty.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.github.pagehelper.PageInfo;
import com.ty.common.utils.R;
import com.ty.entity.Master;
import com.ty.entity.MasterAddress;
import com.ty.service.MasterAddressService;
import com.ty.service.MasterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Master)表控制层
 *
 * @author makejava
 * @since 2020-12-01 22:17:51
 */
@RestController
@RequestMapping("master")
@Slf4j
public class MasterController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private MasterService masterService;
    @Autowired
    private MasterAddressService masterAddressService;

    @PostMapping("page")
    public R getPage(
            Master master,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ){
        PageInfo<Master> pageInfo = this.masterService.getPage(master, page, limit);
        return R.ok().put("page", pageInfo);
    }

    @PostMapping("queryList")
    public R queryList(){
        List<Master> list = this.masterService.getAll();
        return R.ok().put("list",list);
    }

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param master 查询实体
     * @return 所有数据
     */
   /* @GetMapping
    public R selectAll(Page<Master> page, Master master) {
        return success(this.masterService.page(page, new QueryWrapper<>(master)));
    }*/

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    /*@GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.masterService.getById(id));
    }*/

    /**
     * 新增数据
     *
     * @param master 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(Master master) {
        log.info("添加技工传递的数据为{}", master);

        this.masterService.save(master);

        MasterAddress masterAddress = new MasterAddress();
        // TODO 技师地址为固定
        masterAddress.setLng("113.667632");
        masterAddress.setLat("34.75413");
        masterAddress.setMid(master.getId());
        masterAddress.setStatus("1");
        masterAddress.setAddress("河南省郑州市金水区东里路35-9号");
        masterAddressService.save(masterAddress);
        return R.ok();
    }

    /**
     * 修改数据
     *
     * @param master 实体对象
     * @return 修改结果
     */
    /*@PutMapping
    public R update(@RequestBody Master master) {
        return success(this.masterService.updateById(master));
    }
*/
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    /*@DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.masterService.removeByIds(idList));
    }*/
}